package com.basics.api.service.impl;

import com.basics.api.request.user.AuthReq;
import com.basics.api.request.user.SaveNickNameReq;
import com.basics.api.request.user.UpdatePhoneReq;
import com.basics.api.response.user.UserPageInfoData;
import com.basics.api.service.UserApiService;
import com.basics.api.vo.UserInfo;
import com.basics.channel.entity.CyChannelGroupUser;
import com.basics.common.exception.CustomException;
import com.basics.organization.entity.CyOrganizationUser;
import com.basics.user.entity.CyAuth;
import com.basics.user.entity.CyUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserApiServiceImpl extends BaseApiService implements UserApiService {

    @Override
    public UserInfo getUserInfoData(Long userId){
        return getUserInfo(cyUserService.getById(userId));
    }

    @Override
    @Transactional
    public void saveUserBaseInfo(SaveNickNameReq req, Long userId){
        CyUser user = cyUserService.getById(userId);
        user.setNickName(req.getNickName());
        user.setAvatar(req.getAvatar());
        cyUserService.updateById(user);
    }

    @Override
    @Transactional
    public void authentication(AuthReq req, Long userId){
        CyAuth auth = cyAuthService.getByUserId(userId);
        if(auth!=null){
            throw new CustomException("已实名");
        }
        auth = new CyAuth();
        auth.setUserId(userId);
        auth.setRealname(req.getRealname());
        auth.setIdCard(req.getIdCard());
        cyAuthService.save(auth);
    }

    @Override
    public void getOldPhoneCode(String phone,Long userId){
        CyUser user = cyUserService.getById(userId);
        if(!phone.equals(user.getPhone())) {
            throw new CustomException("原手机号错误!");
        }
        getPhoneCode(phone,String.valueOf(userId));
    }

    @Override
    @Transactional
    public void updatePhone(UpdatePhoneReq req, Long userId){
        CyUser user = cyUserService.getById(userId);
        if(!req.getOldPhone().equals(user.getPhone())) {
            throw new CustomException("原手机号错误!");
        }
        if(!checkCode(req.getOldPhone(),String.valueOf(userId),req.getCode())){
            throw new CustomException("验证码错误!");
        }
        CyUser newUser = cyUserService.getByPhone(req.getNewPhone());
        if(newUser!=null){
            throw new CustomException("新手机号已被占用!");
        }
        user.setPhone(req.getNewPhone());
        cyUserService.updateById(user);
    }

    @Override
    public UserPageInfoData getUserPageInfo(Long userId, Long organizationId){
        CyUser user = cyUserService.getById(userId);
        UserPageInfoData data = new UserPageInfoData();
        data.setUserId(userId);
        data.setAvatar(user.getAvatar());
        data.setNickName(user.getNickName());
        data.setPostNum(cyChannelPostService.getPostNumByUserId(userId,organizationId));
        CyOrganizationUser organizationUser = cyOrganizationUserService.getByUserId(userId,organizationId);
        if(organizationUser!=null){
            data.setRemarkName(organizationUser.getRemarkName());
        }
        return data;
    }
}
